package com.cskaoyan.wordmemorize.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.ExcelParseException;
import com.cskaoyan.wordmemorize.dao.entity.VocDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocMapper;
import com.cskaoyan.wordmemorize.excel.SingleExcelData;

import java.util.Map;

public class SingleExcelValidListener extends AnalysisEventListener<SingleExcelData> {

    VocMapper  vocMapper;
    public SingleExcelValidListener(VocMapper vocMapper) {
        this.vocMapper = vocMapper;
    }
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
//        System.out.println("headMap = " + headMap.size());
        if(headMap.size()!=6) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
        if(!headMap.get(0).equals("题目")) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
        if(!headMap.get(1).equals("选项A")) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
        if(!headMap.get(2).equals("选项B")) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
        if(!headMap.get(3).equals("选项C")) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
        if(!headMap.get(4).equals("选项D")) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
        if(!headMap.get(5).equals("正确答案")) throw new ExcelParseException(
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR,
                ResultCodeEnum.VOC_EXCEL_FORMAT_ERROR.getMessage());
    }


    @Override
    public void invoke(SingleExcelData singleExcelData, AnalysisContext analysisContext) {
        System.out.println("singleExcelData = " + singleExcelData);
        //检验是否有null
        if(singleExcelData.getTopic() == null) throw new ExcelParseException(
                ResultCodeEnum.PARAM_ERROR,
                "单元格中有空数据");
        if(singleExcelData.getOptionA() == null) throw new ExcelParseException(
                ResultCodeEnum.PARAM_ERROR,
                "单元格中有空数据");
        if(singleExcelData.getOptionB() == null) throw new ExcelParseException(
                ResultCodeEnum.PARAM_ERROR,
                "单元格中有空数据");
        if(singleExcelData.getOptionC() == null) throw new ExcelParseException(
                ResultCodeEnum.PARAM_ERROR,
                "单元格中有空数据");
        if(singleExcelData.getOptionD() == null) throw new ExcelParseException(
                ResultCodeEnum.PARAM_ERROR,
                "单元格中有空数据");
        if(singleExcelData.getAnswer() == null) throw new ExcelParseException(
                ResultCodeEnum.PARAM_ERROR,
                "单元格中有空数据");
        QueryWrapper<VocDO> vocDoQueryWrapper = new QueryWrapper<VocDO>();
        String answer = singleExcelData.getAnswer();
        //检验正确答案是否在1~4内
        char[] charArray = singleExcelData.getAnswer().toCharArray();
        for (char c : charArray) {
            if (c < '0' || c > '9') throw new ExcelParseException(
                    ResultCodeEnum.VOC_EXCEL_PARSE_ERROR,
                    "正确答案不是数字");
        }
        int answerIndex = Integer.parseInt(answer);
        if (answerIndex < 1 || answerIndex > 4) {
            throw new ExcelParseException(
                    ResultCodeEnum.VOC_EXCEL_PARSE_ERROR,
                    "正确答案不在1~4内");
        }
        //检验单词是否存在
        switch  (answerIndex) {
            case 1:
                answer = singleExcelData.getOptionA();
                break;
            case 2:
                answer = singleExcelData.getOptionB();
                break;
            case 3:
                answer = singleExcelData.getOptionC();
                break;
            case 4:
                answer = singleExcelData.getOptionD();
                break;
          }
        vocDoQueryWrapper.eq("content",answer);
        VocDO vocDO = vocMapper.selectOne(vocDoQueryWrapper);
        if (vocDO == null) {
            throw new ExcelParseException(
                    ResultCodeEnum.VOC_EXCEL_PARSE_ERROR,
                    "单词不存在");
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
